package com.hcht.zjscanService.web;


import com.hcht.zjscanService.dao.MarkItemDao;
import com.hcht.zjscanService.entity.Dict;
import com.hcht.zjscanService.entity.MarkItem;
import com.hcht.zjscanService.serviceImpl.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by Administrator on 2017/7/3.
 */
@Controller
//@RequestMapping(value = "/phone")
public class htmlController {
    private Logger logger = Logger.getLogger(htmlController.class);
    @Autowired
    MarkItemServiceImpl markItemService;

    @Autowired
    DictServiceImpl dictService;

    @RequestMapping(value = "{id}")
    public String info(@PathVariable("id") String id, RedirectAttributes redirectAttributes,Model model) {

        MarkItem markItem = new MarkItem();
        markItem.setShortqrcode(id);
        List<MarkItem>  markItems = markItemService.findList(markItem);
        if(markItems == null || markItems.size() == 0){
            //错误的查询码
            markItem.setActiveflag("-1");
            model.addAttribute("message","此二维码无效。");
            return "infoerror";
        }else if(markItems.size() >= 1){
            markItem = markItems.get(0);

            if(markItem.getActiveflag().equals("0") || markItem.getActiveflag().equals("9")){
                model.addAttribute("message","此二维码未激活或已作废。");
                return "infoerror";
            }

            Dict dict = new Dict();

            dict.setType("item_brand");
            dict.setValue(markItem.getBrand());
            List<Dict> dicts = dictService.findDictList(dict);
            model.addAttribute("brand",dicts.get(0).getLabel());
            model.addAttribute("zhongbrand",dicts.get(0).getDescription());

            dict = new Dict();
            dict.setType("item_kind");
            dict.setValue(markItem.getTypes());
            dicts = dictService.findDictList(dict);
            model.addAttribute("type",dicts.get(0).getLabel());

//            dict = new Dict();
//            dict.setType("item_coninfo");
//            dict.setValue("1");
//            dicts = dictService.findDictList(dict);
//            model.addAttribute("phone",dicts.get(0).getLabel());
//            model.addAttribute("tel",dicts.get(1).getLabel());


//            dict = new Dict();
//            dict.setType("item_coninfo");
//            dict.setValue("2");
//            dicts = dictService.findDictList(dict);


        }



        model.addAttribute("markItem",markItem);
        return "info";
    }


//    @RequestMapping(value = "info/{id}")
//    public String detail(@PathVariable("id") String id, RedirectAttributes redirectAttributes,Model model) {
//    	MarkItem ma=new MarkItem();
//    	ma.setId(id);
//        model.addAttribute("markItem", markItem);
//        return "index_2";
//    }
//    @RequestMapping("imgpage/{id}")
//    public String imgpage(@PathVariable("id") String id,HttpServletRequest re,HttpServletResponse response,Model model){
//    	MarkItem markItem=new MarkItem();
//    	markItem.setId(id);
//    	 model.addAttribute("markItem", mm);
//         return "imgpage";
//    }
//    @RequestMapping("showImage/{id}/{type}")
//    public void showImage(@PathVariable("id") String id,@PathVariable("type") String type,HttpServletRequest re,HttpServletResponse response,String pic_addr){//pic_addr:图片路径(d:\\upload\a.jpg)
//    	MarkItem markItem=new MarkItem();
//    	markItem.setId(id);
//    	MarkItem mm=markItemService.getid(markItem);
//
//        response.setContentType("image/*");
//        FileInputStream fis = null;
//        OutputStream os = null;
//        String name="";
//        if("zm".equals(type)){
//        	name=mm.getZhengimg();
//        }
//        if("fm".equals(type)){
//        	name=mm.getFanimg();
//        }
////        if("cz".equals(type)){
////        	name=mm.getCaizhiimg();
////        }
////        if("bq".equals(type)){
////        	name=mm.getBiaoqianimg();
////        }
////        if("qt".equals(type)){
////        	name=mm.getOtherimg();
////        }
//        try {
//            fis = new FileInputStream("D:\\apache-tomcat-7.0.47\\webapps\\jeeplus\\"+name);
//            //fis = new FileInputStream("D:\\apache-tomcat-7.0.47\\webapps\\jeeplus\\"+name);
//            os = response.getOutputStream();
//            int count = 0;
//            byte[] buffer = new byte[1024*8];
//            while ( (count = fis.read(buffer)) != -1 ){
//                os.write(buffer, 0, count);
//                os.flush();
//            }
//        }catch(Exception e){
//            e.printStackTrace();
//        }finally {
//            try {
//                fis.close();
//                os.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
